﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Web7
{
    /// <summary>
    /// 顺序生成GUID
    /// </summary>
    public class SequentialGuid
    {
        [DllImport("rpcrt4.dll", SetLastError = true)]
        static extern int UuidCreateSequential(out Guid guid);

        /// <summary>
        /// 生成顺序的Guid
        /// </summary>
        /// <returns>guid</returns>
        public static Guid NewGuid()
        {
            const int rpcSOk = 0;

            Guid guid;
            int result = UuidCreateSequential(out guid);
            if (result != rpcSOk)
            {
            	var excpt = new InvalidOperationException("不能生成顺序的GUID");
            	excpt.Publish(OperationProcessLevel.Fatal);
				throw excpt;
            }

            return guid;
        }
    }
}
